package com.shrct.businessprocess.product.impl;

import com.google.common.collect.Iterables;
import com.shrct.businessprocess.product.RCAnonymousProductEntry;
import com.shrct.businessprocess.product.RCAnonymousProductEntryEvent;
import com.shrct.businessprocess.product.RCAnonymouseProductEntryRoot;
import com.shrct.businessprocess.product.RCProductEntry;
import com.shrct.businessprocess.product.RCProductEvent;
import com.shrct.businessprocess.product.RCProductMetaType;
import com.shrct.businessprocess.product.RCProductSpecification;
import com.shrct.businessprocess.product.impl.RCAnonymousProductEntryEventImpl;
import com.shrct.businessprocess.product.impl.RCAnonymouseProductEntryRootImpl;
import com.shrct.businessprocess.product.impl.RCProductEntryImpl;
import com.shrct.businessprocess.product.proxy.RCAnonymousProductEntryProxy;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;

@Entity
@DiscriminatorValue("RCANONYMOUSPRODUCTENTRY_TYPE")
public class RCAnonymousProductEntryImpl extends RCProductEntryImpl implements RCAnonymousProductEntry {
  public RCAnonymousProductEntryImpl() {
    super();
  }
  
  public RCAnonymousProductEntryImpl(final RCAnonymouseProductEntryRoot entryRoot) {
    super();
    this.entryRoot = entryRoot;
  }
  
  @OneToMany(targetEntity = RCAnonymousProductEntryEventImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "product")
  private Collection<RCAnonymousProductEntryEvent> trans = new java.util.ArrayList<com.shrct.businessprocess.product.RCAnonymousProductEntryEvent>();;
  
  public Collection<RCAnonymousProductEntryEvent> getTrans() {
    return this.trans;
  }
  
  public RCAnonymousProductEntry removeAllTrans() {
    for(RCAnonymousProductEntryEvent obj : this.trans){
    	getObjectFactory().delete(obj);
    }
    this.trans.clear();
    return this;	
    
  }
  
  public RCAnonymousProductEntry removeFromTrans(final RCAnonymousProductEntryEvent rCAnonymousProductEntryEvent) {
    this.trans.remove(rCAnonymousProductEntryEvent);
    getObjectFactory().delete(rCAnonymousProductEntryEvent);
    return this;
    
  }
  
  @JoinColumn(name = "entryRoot_id")
  @ManyToOne(targetEntity = RCAnonymouseProductEntryRootImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCAnonymouseProductEntryRoot entryRoot;
  
  public RCAnonymouseProductEntryRoot getEntryRoot() {
    return this.entryRoot;
  }
  
  public RCAnonymousProductEntry setEntryRoot(final RCAnonymouseProductEntryRoot entryRoot) {
    this.entryRoot = entryRoot;
    return this;			
    
  }
  
  private Integer count;
  
  public Integer getCount() {
    return this.count;
  }
  
  public RCAnonymousProductEntry setCount(final Integer count) {
    this.count = count;
    return this;			
    
  }
  
  public String getEntityName() {
    RCAnonymousProductEntry _hostEntry = this.entryRoot.getHostEntry();
    RCProductSpecification _type = _hostEntry.getType();
    String _entityName = _type.getEntityName();
    return _entityName;
  }
  
  public Collection<? extends RCProductEvent> getEvents() {
    return this.trans;
  }
  
  public RCProductMetaType getProductMetaType() {
    RCProductSpecification _type = this.getType();
    RCProductMetaType _productMetaType = _type.getProductMetaType();
    return _productMetaType;
  }
  
  public Iterable<RCProductEntry> allRelatedProducts() {
    Collection<RCAnonymousProductEntry> _entries = this.entryRoot.getEntries();
    Iterable<RCProductEntry> _filter = Iterables.<RCProductEntry>filter(_entries, RCProductEntry.class);
    return _filter;
  }
  
  public static RCAnonymousProductEntry create(final RCAnonymouseProductEntryRoot entryRoot, final IObjectFactory objectFactory) {
    com.shrct.businessprocess.product.RCAnonymousProductEntry rCAnonymousProductEntry = new com.shrct.businessprocess.product.impl.RCAnonymousProductEntryImpl(
    	entryRoot
    );
    objectFactory.create(rCAnonymousProductEntry);
    return rCAnonymousProductEntry;			
    
  }
  
  public static RCAnonymousProductEntry createRCAnonymousProductEntryImpl(final RCAnonymouseProductEntryRoot entryRoot, final IObjectFactory objectFactory, final Procedure1<RCAnonymousProductEntry> init) {
    com.shrct.businessprocess.product.RCAnonymousProductEntry rCAnonymousProductEntry = new com.shrct.businessprocess.product.impl.RCAnonymousProductEntryImpl(
    	entryRoot
    );
    objectFactory.create(rCAnonymousProductEntry);
    init.apply(rCAnonymousProductEntry);
    
    return rCAnonymousProductEntry;			
    
  }
  
  public RCAnonymousProductEntryProxy toProxy() {
    com.shrct.businessprocess.product.proxy.RCAnonymousProductEntryProxy proxy = new com.shrct.businessprocess.product.proxy.RCAnonymousProductEntryProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
